package com.group.itms.routes.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.group.itms.routes.data.BObject;

public class RouteListActivity extends BaseActivity {

	private BObject mLocation; 
	
	public static Intent getIntent(Context c, int locationId) {
		Intent intent = new Intent(c, RouteListActivity.class);
		intent.putExtra(OBJECT_ID, locationId);
		return intent;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args = getIntent().getExtras();
		int locId = args.getInt(OBJECT_ID);
		mLocation = getObject(locId);
		final ArrayAdapter<BObject> routeAdapter = new ArrayAdapter<BObject>(
				this, android.R.layout.simple_list_item_1, mLocation.getChildrenRoutes()) {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View v = super.getView(position, convertView, parent);
				if (v instanceof TextView) {
					BObject route = getItem(position);
					((TextView)v).setText(route.getNameInd());
				}
				return v;
			}
			
		};
		ListView routesListView = new ListView(this);
		routesListView.setAdapter(routeAdapter);
		routesListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				BObject route = routeAdapter.getItem(position);
				Intent intent = RouteActivity.getIntent(RouteListActivity.this, route.getId());
				intent.putExtra("locName", mLocation.getName());
				startActivity(intent, true);
			}
		});
		setContentView(routesListView);
		setTitle(mLocation.getName());
	}

	@Override
	protected String getTag() {
		return "RouteListActivity";
	}
}
